import java.awt.*;
import java.awt.event.*;
import java.util.*;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.LinkedList;
import javax.swing.*;


public class DrawGraph extends JPanel 
{
        private GraphPanel graphPanel1;
        
        Vertex<Integer, Node> vertexid1;
        Vertex<Integer, Node> vertexid2;
        Edge<Integer> edge1;

        private JButton addvertex;
        private JButton addedge;
        private JButton removeVertex;
        private JButton removeEdge;
        private JButton exportfile;
    
        int x1,y1,x2,y2;
        int nodeCount, id1, id2;
        private Node node1,  node2;

        Graph<Integer> graph1 = new Graph<Integer>();
    
        public DrawGraph()
        {
                setLayout(null);
                addButtonsPanel();
        
                graphPanel1 = new GraphPanel();
                graphPanel1.setLocation(0, 300);
                graphPanel1.setBounds(300, 0, 600, 600);
                graphPanel1.setBackground(Color.white);
                graphPanel1.setBorder(BorderFactory.createTitledBorder("Draw Graph"));
                graphPanel1.addMouseListener(new MouseHandler());
                add(graphPanel1);
        }
        private void addButtonsPanel()
        {
                JPanel pan = new JPanel(new GridLayout(5,1));
                pan.setLocation(0, 0);
                pan.setBounds(0, 0, 290, 300);
                        
                addvertex = new JButton("ADD VERTEX");
                //addvertex.addActionListener(new DrawGraph());
                pan.add(addvertex);
     
                addedge = new JButton("ADD EDGE");
                pan.add(addedge);
        
                removeVertex = new JButton("REMOVE VERTEX");
                pan.add(removeVertex);
        
                removeEdge = new JButton("REMOVE EDGE");
                pan.add(removeEdge);
        
                exportfile = new JButton("EXPORT TO FILE");
                pan.add(exportfile);
                add(pan);
        
          
        }
        private class MouseHandler extends MouseAdapter 
        {
       /* public void actionPerformed(ActionEvent event) 
	{
 		 		
		if (event.getSource() == addedge ) 
		{	
			
			 addvertex.setEnabled(false);	
 		
		}
         }   */
                public void mouseClicked(MouseEvent event)
                {
                        if(event.isMetaDown())
                        {
                        }
                        else if(event.isAltDown())
                        {
                        }
                        else
                        {
            	        /*int modifiers = event.getModifiers();
        	        if ((modifiers & InputEvent.BUTTOnode1_MASK) == InputEvent.BUTTOnodeN1_MASK) 
        	        {
          	
          	        addvertex.setEnabled(false);*/
          	             
                        int xPos = event.getX();
                        int yPos = event.getY();
                                if(canAddNode(xPos,yPos))
                                {
                                        nodeCount++;
                                        Node n = new Node(xPos,yPos);
                                                                   
                                        vertexid1 = new Vertex<Integer, Node>(nodeCount,n);
                                        graph1.addVertex(vertexid1);
                                }
                                repaint();
                        }
                }
                public void mousePressed(MouseEvent event)
                {
                        x1 = event.getX();
                        y1 = event.getY();
               
                        id1 = getNodeId(x1, y1); 
                        if(id1>=0) 
                        {
                    	        vertexid1 = graph1.getVertexNode(id1);
                    	        //node1 = (Node)vertexid1.getVertextInfo();
                        }
                }
                public void mouseReleased(MouseEvent event)
                {
                        if(event.isMetaDown())
                        { 
                        }
                        else if(event.isAltDown())
                        {
                        }
                        else
                        {
                                x2 = event.getX();
                                y2 = event.getY();
                                id2 = getNodeId(x2, y2);
                                if(id2>=0) 
                                {
                                        vertexid2 = (Vertex)graph1.getVertexNode(id2);
                                        //node2 = (Node)vertexid2.getVertextInfo(); 
                                }
                                if(id1>=0 && id2 >=0 && id1 != id2 )
                                {
                                        Edge<Integer> edge1 = new Edge<Integer>(vertexid1, vertexid2);
                                        graph1.addEdge(edge1);
                                 }           
                                graphPanel1.repaint();
                        }
                }
        }
        private boolean canAddNode(int x1, int y2)
        {
                for(int i=1; i<=graph1.getNoOfVertices(); i++)
                {
                        vertexid2 = (Vertex)graph1.getVertexNode(i);
                        Node n = (Node)vertexid2.getVertextInfo();
                         
                        double x = n.getX();
                        double y= n.getY();
            
                        if(x1>=x && (x1<=x+20) && y1>=y && (y1<=y+20))
                                return false;
                }
                return true;
        }
        private int getNodeId(int x1, int y1)
        {
                for(int i=1; i<=graph1.getNoOfVertices(); i++)
                {
                        vertexid2 = (Vertex)graph1.getVertexNode(i);
                        Node n = (Node)vertexid2.getVertextInfo(); 
                        int n1 = vertexid2.getVertexId();
                              
                        int x = (int) n.getX();
                        int y = (int) n.getY();
            
            
                        if((x1<=x+20)&& (x1>=x) && (y1<=y+20)&&(y1>=y)) 
                            return n1;
                }
                return -1; 
        }
    
        private class GraphPanel extends JPanel
        {
                public GraphPanel()
                {
                        setLayout(null);
                }
        
                public void paintComponent(Graphics g)
                {
                        super.paintComponent(g);
            
                        g.setColor(Color.black);
                        drawEdges(g);
            
                        g.setColor(Color.black);
                        for(int i=1; i<=graph1.getNoOfVertices();i++)
                        {
                                vertexid2 = (Vertex)graph1.getVertexNode(i);
                                Node n = (Node)vertexid2.getVertextInfo();
                                int n1 = vertexid2.getVertexId();
                                
                                int x = (int)n.getX();
                                int y = (int)n.getY();
                                g.fillOval(x, y, 10, 10);
                                g.drawString(n1+"", x, y);
                         }
                }
                public void drawEdges(Graphics g)
                {
                        for(int i=1; i<=graph1.getNoOfVertices(); i++)
                        {
                                for(int k=2; k<=graph1.getNoOfVertices();k++)
                                {        
                                        vertexid1 = (Vertex)graph1.getVertexNode(i);
                                        vertexid2 = (Vertex)graph1.getVertexNode(k);
                                                       
                	                edge1 = graph1.getEdgeNode(vertexid1,vertexid2);
                	                if(edge1!= null)
                	                {
                	                        vertexid1 = edge1.getVertexOne();
                	                        vertexid2 = edge1.getVertexTwo();
                	
                	                        Node node1 = (Node)vertexid1.getVertextInfo();
                	                        Node node2 = (Node)vertexid2.getVertextInfo();
                	
                	                        int x1 = (int) node1.getX();
                	                        int y1 = (int) node1.getY();
                	
                	                        int x2 = (int) node2.getX();
	                                        int y2 = (int) node2.getY();
        	                                g.drawLine(x1+10, y1+10, x2+10, y2+10);
                	                }
                                }
                        }
                }
        }
 }
